package com.ztjava.core;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.Routes;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

public abstract class HPlugin {
	public abstract void configConstant(Constants me);
	public abstract void configRoute(Routes me);
	public abstract void configArp(ActiveRecordPlugin arp);
	public abstract void configInterceptor(Interceptors me);
	public abstract void configHandler(Handlers me);
	public abstract void afterJFinalStart();
	
	public void addMapping(ActiveRecordPlugin arp,String tablename ,Class<? extends Model<?>>  classType){
		arp.addMapping(tablename, classType);
		try{
			HModel model = (HModel)classType.newInstance();
			String[][] columnsArr = model.getColumns();
			String[] columnArr;
			List<Record> columnList = new ArrayList<Record>();
			for(int i=0;i<columnsArr.length;i++){
				Record column = new Record();
				columnArr = columnsArr[i];
				column.set("name", columnArr[0]);
				column.set("type", columnArr[1]);
				column.set("length", columnArr[2]);
				if(columnArr.length>3){
					column.set("default",columnArr[3]);
				}
				columnList.add(column);
			}
			Constant.pluginTable.put(tablename, columnList);
		}catch (Exception e){
			e.printStackTrace();
		}

	}
}
